import requests
from tqdm import tqdm  # 需 pip install tqdm

headers = {
    "accept": "*/*",
    "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,ar;q=0.7",
    "cache-control": "no-cache",
    "pragma": "no-cache",
    "priority": "i",
    "range": "bytes=0-",  # 支持断点续传
    "referer": "https://www.vjshi.com/watch/33880253.html?from=search",
    "sec-ch-ua": "\"Not)A;Brand\";v=\"8\", \"Chromium\";v=\"138\", \"Microsoft Edge\";v=\"138\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\"",
    "sec-fetch-dest": "video",
    "sec-fetch-mode": "no-cors",
    "sec-fetch-site": "same-site",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0"
}
cookies = {
    "acw_sc__v2": "68917a29809fc88d29174db638a91e5363788dfb",
    "browserId": "9368bd04e5201e086d27e502b11a5c49",
    "Hm_lvt_d50ee9203a35be45d6b0a9db1948f94e": "1754364458",
    "HMACCOUNT": "628427A3BD9CB08E",
    "Hm_up_d50ee9203a35be45d6b0a9db1948f94e": "%7B%22use_new_ui%22%3A%7B%22value%22%3A%221%22%2C%22scope%22%3A1%7D%7D",
    "_ga": "GA1.1.331606996.1754364459",
    "_ga_X670GRBYKT": "GS2.1.s1754364458$o1$g1$t1754365080$j43$l0$h0",
    "Hm_lpvt_d50ee9203a35be45d6b0a9db1948f94e": "1754365085"
}
url = "https://mp4.vjshi.com/2024-06-13/13bbdfe8f5b043bc92b4237fc9e80001.mp4"

local_filename = url.split('/')[-1]  # 文件名自动识别
# 发起GET请求，stream=True
with requests.get(url, headers=headers, cookies=cookies, stream=True) as r:
    r.raise_for_status()
    total = int(r.headers.get('content-length', 0))
    with open(local_filename, 'wb') as f, tqdm(
            desc=local_filename,
            total=total,
            unit='iB',
            unit_scale=True,
            unit_divisor=1024,
    ) as bar:
        for chunk in r.iter_content(chunk_size=8192):
            if chunk:
                f.write(chunk)
                bar.update(len(chunk))
print("下载完成:", local_filename)

